package com.gz.common;

import java.util.List;

import com.gz.common.model.Coupon;
import com.gz.common.model.Integralrecord;
import com.jfinal.plugin.activerecord.Page;

/**
 * 积分记录表service
 * 
 * @author 尹公子
 *
 */
public class IntegralrecordService {

	private static IntegralrecordService service;

	private IntegralrecordService() {
	}

	public static IntegralrecordService getService() {
		if (service == null) {
			service = new IntegralrecordService();
		}
		return service;
	}

	/**
	 * 积分记录表新增数据
	 */
	public boolean xinZengJiFeng(Integralrecord integralrecord) {
		return integralrecord.save();
	}

	/**
	 * 查询用户积分变更记录
	 */
	public List<Integralrecord> findIntegralrecordList(Integer userId) {
		List<Integralrecord> integralrecordList = Integralrecord.dao
				.find("select * from integralrecord where userid=? order by integralid desc", userId);
		return integralrecordList;
	}

	/**
	 * 查询用户积分提现申请记录
	 */
	public List<Integralrecord> findTXrecordList(Integer userId) {
		List<Integralrecord> integralrecordList = Integralrecord.dao
				.find("select * from integralrecord where userid=? and `type`=2  order by integralid desc", userId);
		return integralrecordList;
	}

	/**
	 * 查询所有用户提现申请记录
	 */

	public Page<Integralrecord> findIntegralrecordAll(Integer pageNumber, Integer pageSize, Integer type) {
		if (type != null) {
			return Integralrecord.dao.paginate(pageNumber, pageSize, "select i.*,tb_user.nickname,tb_user.mobile ",
					"from integralrecord i,tb_user where i.userid=tb_user.id and type=? ORDER BY CREATEtime", type);
		}
		return Integralrecord.dao.paginate(pageNumber, pageSize,"select i.*,tb_user.nickname,tb_user.mobile" ,"from integralrecord i,tb_user where i.userid=tb_user.id  ORDER BY CREATEtime ");
	}
	/**
	 * 根据integralid查询申请记录
	 */
	public Integralrecord fingIntegralrecordById(Integer integralid) {
		Integralrecord integralrecord = Integralrecord.dao.findById(integralid);
		return integralrecord;
	}

	/**
	 * 查询所有积分记录按条件
	 */
	public List<Integralrecord> findIntegralrecords(String username1, String order, String username2) {
		StringBuffer sql = new StringBuffer();
		sql.append("select us.username as username1,o.orderSN,o.payFee,i.getintegral,u.username \r\n" + 
				"from integralrecord as i left join tb_order as o\r\n" + 
				"on i.recorduser=o.id left  join tb_user as u \r\n" + 
				"on i.userid=u.id left join tb_user as us on o.userId=us.id \r\n" + 
				"where i.type=1 ");
		// 判断购买商品的用户是否存在
		if (username1 != null&&!username1.equals("")) {
			sql.append("and us.username=?");
			return Integralrecord.dao.find(sql.toString(), username1);
		}
		// 判断获取积分的用户是否存在
		if (username2 != null && !username2.equals("")) {
			sql.append("and u.username=?");
			return Integralrecord.dao.find(sql.toString(), username2);
		}
		// 判断订单编号是否存在
		if (order != null&&!order.equals("")) {
			sql.append("and o.orderSN=?");
			return Integralrecord.dao.find(sql.toString(), order);
		}
		return Integralrecord.dao.find(sql.toString());
	}
}
